import http from '@/utils/http';
import { AddRadioData, AddSelectData, AddTest, ModifyTest } from '@/utils/interface';

export function getRadioList() {
  return http({
    url: '/getSingles.do',
  });
}

export function deleteSelectRadio(ids: number[] | string[]) {
  return http({
    url: '/softDeleteSingle.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function addRadio(data: AddRadioData) {
  return http({
    url: '/addSingle.do',
    method: 'post',
    data: data,
  });
}
interface ModifyRadioData {
  id: number;
  single_name: string;
  A: string;
  B: string;
  C: string;
  D: string;
  right_key: string;
  subject_name: string;
}
export function updateRadio({
  right_key,
  id: key,
  single_name,
  subject_name,
  A,
  B,
  C,
  D,
}: ModifyRadioData) {
  return http({
    url: '/updateSingle.do',
    method: 'post',
    data: {
      key,
      single_name,
      subject_name,
      A,
      B,
      C,
      D,
      right_key,
    },
  });
}

export function getAllSelect() {
  return http({
    url: '/getSelections.do',
  });
}

export function deleteSelect(ids: number[]) {
  return http({
    url: '/softDeleteSelection.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function addSelect(data: AddSelectData) {
  return http({
    url: '/addSelection.do',
    method: 'post',
    data: data,
  });
}
interface ModifySelectData {
  id: number;
  selection_name: string;
  A: string;
  B: string;
  C: string;
  D: string;
  right_key: string;
  subject_name: string;
}
export function updateSelect({
  id: key,
  selection_name,
  subject_name,
  A,
  B,
  C,
  D,
  right_key,
}: ModifySelectData) {
  return http({
    url: '/updateSelection.do',
    method: 'post',
    data: {
      key,
      selection_name,
      subject_name,
      A,
      B,
      C,
      D,
      right_key,
    },
  });
}

export function recycleSingle(ids: number[]) {
  return http({
    url: '/recycleSingle.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function deleteSingle(ids: number[]) {
  return http({
    url: '/deleteSingle.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function deleteSelection(ids: number[]) {
  return http({
    url: '/deleteSelection.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function recycleSelection(ids: number[]) {
  return http({
    url: '/recycleSelection.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function getAllJudges() {
  return http({
    url: '/getJudges.do',
  });
}

export function softDeleteJudge(ids: number[]) {
  return http({
    url: '/softDeleteJudge.do',
    data: [...ids.map((id) => ({ key: id }))],
    method: 'post',
  });
}

export function updateJudge({
  id: key,
  judge_name,
  subject_name,
  right_key,
}: {
  id: number;
  judge_name: string;
  subject_name: string;
  right_key: string;
}) {
  return http({
    url: '/updateJudge.do',
    method: 'post',
    data: {
      key,
      judge_name,
      subject_name,
      right_key,
    },
  });
}

export function addJudge(data: { judge_name: string; subject_name: string; right_key: string }) {
  return http({
    url: '/addJudge.do',
    method: 'post',
    data: data,
  });
}

export function recycleJudge(ids: number[]) {
  return http({
    url: '/recycleJudge.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function deleteJudge(ids: number[]) {
  return http({
    url: '/deleteJudge.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function getTests() {
  return http({
    url: '/geTests.do',
  });
}

export function deleteTest(ids: number[]) {
  return http({
    url: '/deleteTest.do',
    method: 'post',
    data: [...ids.map((id) => ({ key: id }))],
  });
}

export function addTest(data: AddTest) {
  const { singles, selections, judges } = data;
  return http({
    url: '/addTest.do',
    method: 'post',
    data: {
      ...data,
      singles: (singles as number[]).map((item) => {
        return {
          key: item,
        };
      }),
      selections: (selections as number[]).map((item) => {
        return {
          key: item,
        };
      }),
      judges: (judges as number[]).map((item) => {
        return {
          key: item,
        };
      }),
    },
  });
}

export function updateTest(data: ModifyTest) {
  const { singles, selections, judges } = data;
  return http({
    url: '/updateTest.do',
    method: 'post',
    data: {
      ...data,
      key: data.id,
      singles: (singles as number[]).map((item) => {
        return {
          key: item,
        };
      }),
      selections: (selections as number[]).map((item) => {
        return {
          key: item,
        };
      }),
      judges: (judges as number[]).map((item) => {
        return {
          key: item,
        };
      }),
    },
  });
}
